<template>
  <BasicModal v-bind="$attrs" @register="registerModal" title="收银台配置" :showOkBtn="false" width="1000px" destroyOnClose>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <a-button type="primary" preIcon="ant-design:plus-outlined" @click="handleCreate"> 新增</a-button>
      </template>
      <template #action="{ record }">
        <TableAction :actions="getTableAction(record)"/>
      </template>
    </BasicTable>
  </BasicModal>
  <CashierConfigForm @register="registerCashierConfigForm" @success="reload" />
</template>
<script lang="ts" setup>
  import {ref,toRaw,unref} from 'vue';
  import {BasicModal, useModalInner} from '/@/components/Modal';
  import {BasicTable, useTable, TableAction} from '/@/components/Table';
  import { cashierConfigColumns } from './payPassage.data';
  import { listCashierConfig, deleteCashierConfig } from './payPassage.api';
  import CashierConfigForm from './CashierConfigForm.vue';
  import { useModal } from '/@/components/Modal';
  const emit = defineEmits(['success', 'register']);
  const checkedKeys = ref<Array<string | number>>([]);

  const appId = ref<any>()
  const [registerCashierConfigForm, { openModal: openCashierConfigForm }] = useModal();
  const [registerTable, { reload }] = useTable({
    api: listCashierConfig,
    columns: cashierConfigColumns,
    beforeFetch: (params) => {
      params.appId = appId.value
    },
    rowKey: 'id',
    striped: true,
    useSearchForm: false,
    immediate: false,
    bordered: false,
    showIndexColumn: false,
    pagination: false,
    tableSetting: {fullScreen: true},
    canResize: false,
    actionColumn: {
      width: 150,
      title: '操作',
      dataIndex: 'action',
      slots: {customRender: 'action'},
      fixed: undefined,
    },
  })

  function getTableAction(record) {
    return [
      {
        label:"编辑",
        onClick: handleSettingTag.bind(null, record),
      },
      {
        label: '删除',
        color: 'error',
        // auth: 'pay:DeleteCashierConfig',
        popConfirm: {
          title: '是否确认删除',
          confirm: handleDelete.bind(null, record),
        },
      }
    ]
  }
  const handleDelete = (record) => {
    deleteCashierConfig({ id: record.id }, () => reload())
  }

  const handleCreate = () => {
    openCashierConfigForm(true, { record: { appId: appId.value }, isUpdate: false })
  }

  const handleSettingTag = (record) => {
    openCashierConfigForm(true, { record: { appId: appId.value, ...record }, isUpdate: true})
  }

  const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
    console.log('data', data)
    appId.value = data.appId
    reload()
  });

</script>
